#!/usr/bin/python
# -*- coding: UTF-8 -*-

##
import os, sys
import gtk, gobject
import gtkmozembed
import gettext
_ = gettext.gettext


Windows = []
class mainwindow:
    def __init__(self, mozeditdir='', url='about:blank'):
        Windows.append(self)
        self.accel_group = gtk.AccelGroup()
        self.mainwindow = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.mainwindow.set_title(_("WebRunning"))
        self.mainwindow.resize(750,550)
        self.mainwindow.set_position(gtk.WIN_POS_NONE)
        self.mainwindow.add_accel_group(self.accel_group)
        self.mainwindow.show()
        self.mainwindow.connect("delete_event", self.on_close)

        self.vbox1 = gtk.VBox(False, 0)
        self.vbox1.show()
        self.vbox1.set_spacing(0)

        ###
        if not mozeditdir:
            homedir = os.environ['HOME']
            mozeditdir = os.path.join(homedir,'.mozilla')
        if not os.path.isdir(mozeditdir):
            os.mkdir(mozeditdir)
        if not os.path.isdir(mozeditdir+'/webrunning'):
            os.mkdir(mozeditdir+'/webrunning')
        gtkmozembed.set_profile_path(mozeditdir, 'webrunning')

        self.gtkmozembed = gtkmozembed.MozEmbed()
        self.gtkmozembed.show()

        self.gtkmozembed.connect('button-press-event', self.on_popmenu)
        self.gtkmozembed.connect('new-window', self.on_new_window)
        self.gtkmozembed.connect('size-to', self.on_size)
        self.gtkmozembed.connect('title', self.on_title)
        self.gtkmozembed.connect('link-message', self.on_link_message)
        ###
        #self.vbox1.pack_start(self.gtkmozembed, False, False, 0)

        self.frame1 = gtk.Frame()
        self.frame1.show()
        self.frame1.set_shadow_type(gtk.SHADOW_NONE)
        self.frame1.add(self.gtkmozembed)

        self.vbox1.pack_start(self.frame1, True, True, 0)
        
        self.statusbar = gtk.Statusbar()
        self.statusbar.set_has_resize_grip(True)
        self.statusbar.show()
        self.statusbar.connect("button-press-event", self.on_popmenu)

        #self.statuslable = gtk.Label()
        #self.statuslable.show()
        #self.statusbar.add(self.statuslable)

        self.vbox1.pack_start(self.statusbar, False, False, 0)

        self.mainwindow.add(self.vbox1)


        ##
          
        self.popmenu = gtk.Menu()
        self.back = gtk.ImageMenuItem(_("gtk-go-back"))
        self.back.show()
        self.back.connect("activate", self.on_back)

        self.popmenu.append(self.back)

        self.forward = gtk.ImageMenuItem(_("gtk-go-forward"))
        self.forward.show()
        self.forward.connect("activate", self.on_forward)

        self.popmenu.append(self.forward)

        self.reload = gtk.ImageMenuItem(_("gtk-refresh"))
        self.reload.show()
        self.reload.connect("activate", self.on_reload)

        self.popmenu.append(self.reload)

        self.stop = gtk.ImageMenuItem(_("gtk-stop"))
        self.stop.show()
        self.stop.connect("activate", self.on_stop)

        self.popmenu.append(self.stop)


    def on_close(self, *arg):
        "on_close activated"
        Windows.remove(self)
        self.mainwindow.destroy()
        if not Windows:
            gtk.main_quit()

    def on_quit(self, obj):
        "on_quit activated"
        #print 'on_quit activated'
        #print Windows
        t = reversed(Windows)
        t =  Windows
        for i in t:
            self.on_close()
        gtk.main_quit()


    def on_link_message(self, *arg):
        link = self.gtkmozembed.get_link_message()
        self.statusbar.push(0, link)
    
    def on_title(self, *arg):
        title = self.gtkmozembed.get_title()
        print 'title:', title
        self.mainwindow.set_title(title)

    def on_new_window(self, mozembed, retval, chromemask):
        print mozembed, retval, chromemask
        window = mainwindow()
        retval = window.gtkmozembed
        #return window.gtkmozembed

    def on_size(self, x, y):
        self.mainwindow.resize(x+50, y)

    def on_popmenu(self, widget, event):
        self.popmenu.popup(None, None, None, 0, event.time)
    #def on_popmenu(self, *arg):
    #    print arg
    #    self.popmenu.popup(None, None, None, 0, 0)

    def on_back(self, widget):
        self.gtkmozembed.go_back()
    
    def on_forward(self, widget):
        self.gtkmozembed.go_forward()

    def on_reload(self, widget):
        self.gtkmozembed.reload(gtkmozembed.FLAG_RELOADNORMAL)

    def on_stop(self, widget):
        self.gtkmozembed.stop_load()

    def get_custom_widget(self, id, string1, string2, int1, int2):
        w = gtk.Label(_("(custom widget: %s)") % id)
        return w
## end mainwindow



def main():
    arg = list(sys.argv[1:])
    for i in arg:
        if not i.startswith('-'):
            url = i
            win = mainwindow()
            win.gtkmozembed.load_url(url)
    gtk.main()

##cmd test
if __name__ == '__main__':
    main()
